package com.woniuxy.yoga.coach.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.yoga.coach.dao.mapper.CoachLogMapper;
import com.woniuxy.yoga.coach.dao.model.CoachLog;
import com.woniuxy.yoga.coach.model.param.QueryLoginLogParam;
import com.woniuxy.yoga.coach.service.service.CoachLogService;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 教练日志表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class CoachLogServiceImpl extends ServiceImpl<CoachLogMapper, CoachLog> implements CoachLogService {

    @Autowired
    private CoachLogMapper coachLogMapper;

    /**
     * 教练登录日志
     * @param param
     * @return
     */
    @Override
    public PageInfo queryLoginLog(QueryLoginLogParam param) {
        QueryWrapper<CoachLog> wrapper = new QueryWrapper<>();
        //起始时间搜索
        if (ObjectUtil.isNotEmpty(param.getStartLogTime())){
            wrapper.le("log_time",param.getStartLogTime());
        }
        //结束时间搜索
        if (ObjectUtil.isNotEmpty(param.getEndLogTime())){
            wrapper.ge("log_time",param.getEndLogTime());
        }
        //教练姓名搜索
        if (ObjectUtil.isNotEmpty(param.getLogCoach())){
            wrapper.like("loc_coach",param.getLogCoach());
        }
        //索引排序
        wrapper.orderByAsc("log_id");
        //分页查询
        Page<CoachLog> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        coachLogMapper.selectPage(page,wrapper);
        //对象转换
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(page.getRecords());
        return pageInfo;
    }
}
